﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Fusion
{
    // A completely transparent Directional Menu, designed to be updated and drawn inside another class
    public class DirectionalMenu
    {
        // Data
        public List<List<string>> columns;
        public List<int> currentRows;
        public int currentColumn;

        // Input timers
        public int upCounter;
        public int downCounter;
        public int leftCounter;
        public int rightCounter;

        // Events
        public event IntDelegate Moved;
        public event IntDelegate Changed;
        public event VoidDelegate LeftEdge;
        public event VoidDelegate RightEdge;
        public event VoidDelegate TopEdge;
        public event VoidDelegate BottomEdge;

        // Constructor
        public DirectionalMenu()
        {
            columns = new List<List<string>>();
            currentRows = new List<int>();
            currentColumn = 0;
        }

        // Adds an entry to the menu
        public void Add( List<string> entry )
        {
            columns.Add( entry );
            currentRows.Add( 0 );
        }

        // returns the current selection at a particular index
        public string Selection( int index )
        {
            if( index < 0 || index >= currentRows.Count )
                return default( string );
            else
                return columns[index][currentRows[index]];
        }

        // Checks input variables and adds to the local input timers
        // Call this once directly before every Update()
        public void Input( bool up, bool down, bool left, bool right )
        {
            if( up ) upCounter++; else upCounter = 0;
            if( down ) downCounter++; else downCounter = 0;
            if( left ) leftCounter++; else leftCounter = 0;
            if( right ) rightCounter++; else rightCounter = 0;
        }

        // Updates the menu
        public void Update()
        {
            // Move up
            if( upCounter == 1 )
            {
                currentColumn--;

                if( currentColumn < 0 )
                {
                    currentColumn = 0;
                    TopEdge();
                }
                else
                    Moved( currentColumn );
            }

            // Move down
            if( downCounter == 1 )
            {
                currentColumn++;

                if( currentColumn >= columns.Count )
                {
                    currentColumn = columns.Count - 1;
                    BottomEdge();
                }
                else
                    Moved( currentColumn );
            }

            // there are no options in the current menu entry, so gtfo!
            if( currentRows.Count <= 0 )
                return;

            // Move left
            if( leftCounter == 1 )
            {
                currentRows[currentColumn]--;

                if( currentRows[currentColumn] < 0 )
                {
                    currentRows[currentColumn] = 0;
                    LeftEdge();
                }
                else
                    Changed( currentRows[currentColumn] );
            }

            // Move right
            if( rightCounter == 1 )
            {
                currentRows[currentColumn]++;

                if( currentRows[currentColumn] >= columns[currentColumn].Count )
                {
                    currentRows[currentColumn] = columns[currentColumn].Count - 1;
                    RightEdge();
                }
                else
                    Changed( currentRows[currentColumn] );
            }
        }
    }
}
